import { login, logout, getDictConfig, getCurrentUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    dictConfig: {
      caseType: [],
      elementType: [],
      keyword: {},
      locationBy: {},
      stepControl: {},
      stepType: []
    },
    realName: '',
    username: '',
    userId: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_REAL_NAME: (state, name) => {
    state.realName = name
  },
  SET_USER_NAME: (state, name) => {
    state.username = name
  },
  SET_USER_ID: (state, id) => {
    state.userId = id
  },
  SET_DICT_CONFIG: (state, config) => {
    state.dictConfig.caseType = config.case_type
    state.dictConfig.elementType = config.element_type
    state.dictConfig.stepType = config.step_type
    state.dictConfig.keyword = config.keyword
    config.location_by.forEach(function(obj) {
      state.dictConfig.locationBy[obj.id] = obj
    })
    config.step_control.forEach(function(obj) {
      state.dictConfig.stepControl[obj.id] = obj
    })
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), passwd: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getConfig({ commit, state }) {
    return new Promise((resolve, reject) => {
      getDictConfig().then(response => {
        commit('SET_DICT_CONFIG', response.data)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getCurrentUserInfo().then(response => {
        const { data } = response

        const { real_name, user_name, id } = data

        commit('SET_REAL_NAME', real_name)
        commit('SET_USER_NAME', user_name)
        commit('SET_USER_ID', id)

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    // return new Promise((resolve, reject) => {
    //   logout().then(() => {
    //     removeToken() // must remove  token  first
    //     resetRouter()
    //     commit('RESET_STATE')
    //     resolve()
    //   }).catch(error => {
    //     reject(error)
    //   })
    // })
    commit('SET_TOKEN', '')
    removeToken() // must remove  token  first
    resetRouter()
    commit('RESET_STATE')
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
